//package com.zhijiang.common.config.security;
//
//
//import cn.hutool.core.util.StrUtil;
//import com.zhijiang.common.util.jwt.JwtUtil;
//import com.zhijiang.common.util.redis.RedisCache;
//import com.zhijiang.common.util.redis.RedisConstants;
//import com.zhijiang.hospital.entrty.LoginUser;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * 过滤器
// */
//@Slf4j
//@Component
//public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
//
//
//    @Autowired
//    private RedisCache redisCache;
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getHeader("token");
//        log.info("=========================="+token);
//        //如果token不为null 或 “” 则放行
//        if (StringUtils.isEmpty(token)){
//            response.setHeader("Access-Control-Allow-Origin", "*");//所有请求
//            response.setHeader("Access-Control-Allow-Methods", "");
//            response.setHeader("Access-Control-Max-Age", "1728000");
//            response.setHeader("Access-Control-Allow-Credentials", "true");
//            response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//
//            filterChain.doFilter(request, response);
//            return;
//        }
//        //解析token
//        String userId;
//        try{
//
//            userId = JwtUtil.parseJWT(token).getSubject();
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new RuntimeException("token非法");
//        }
//        //从redis中获取用户信息
//        LoginUser loginUser = redisCache.getCacheObject(RedisConstants.LOGIN + userId);
//        if (StrUtil.isEmptyIfStr(loginUser)){
//            throw new RuntimeException("用户未登录");
//        }
//        boolean expires = redisCache.getExpires(RedisConstants.LOGIN + userId);
//        System.out.println(expires);
//        if (!expires){
//          throw   new RuntimeException("用户验证已过期");
//        }
//        redisCache.expire(RedisConstants.LOGIN + userId,RedisConstants.LOGIN_CODE_TTL,RedisConstants.LOGINUNIT);
//        //TODO 获取权限信息封装到Authentication中
//        UsernamePasswordAuthenticationToken to = new UsernamePasswordAuthenticationToken(loginUser, null, null);
//        SecurityContextHolder.getContext().setAuthentication(to);
//        //方行
//        filterChain.doFilter(request,response);
//    }
//}
